/**
 ******************************************************************************
 * @file    lptmr.c
 * @author  hyseim software Team
 * @date    21-Mar-2024
 * @brief   This file provides all the tmp functions.
 ******************************************************************************
 * @attention
 *
 * Copyright (c) 2020 Hyseim. Co., Ltd.
 * All rights reserved.
 *
 * This software is licensed under terms that can be found in the LICENSE file
 * in the root directory of this software component.
 * If no LICENSE file comes with this software, it is provided AS-IS.
 *
 ******************************************************************************
 */

/* Includes ------------------------------------------------------------------*/
#include "lptmr.h"

/** @addtogroup IM110GW_Driver
 * @{
 */

/** @defgroup LPTMR
 * @brief LPTMR driver modules
 * @{
 */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/** @defgroup LPTMR_Private_Functions
  * @{
  */

/**
 * @brief  Initialize LPTMR.
 * @param  LPTMR_InitStruct: pointer to a LPTMR_Init_t structure
 *         that contains the configuration information for the specified 
 *         LPTMR peripheral.
 * @return None
 */
void LPTMR_Init(LPTMR_Init_t *LPTMR_InitStruct)
{
    LPTMR->CSR = LPTMR_InitStruct->LPTMR_WorkMode | LPTMR_InitStruct->LPTMR_CountMode |
                 LPTMR_InitStruct->LPTMR_PinPolarity | LPTMR_InitStruct->LPTMR_InterruptEnable |
                 LPTMR_InitStruct->LPTMR_DmaRequest;

    LPTMR->CMR = LPTMR_InitStruct->LPTMR_CompareValue;

    if (LPTMR_InitStruct->LPTMR_BypassPrescaler == LPTMR_Prescaler_Disable)
    {
        LPTMR->PSR = LPTMR_InitStruct->LPTMR_BypassPrescaler;
    }
    else
    {
        LPTMR->PSR = LPTMR_InitStruct->LPTMR_BypassPrescaler | LPTMR_InitStruct->LPTMR_Prescaler;  
    }
}

/**
 * @brief  Fills each LPTMR_StructInit member with its default value.
 * @param  LPTMR_StructInit: pointer to a LPTMR_Init_t structure which
 *         will be initialized.
 * @return None
 */
void LPTMR_StructInit(LPTMR_Init_t *LPTMR_StructInit)
{
    LPTMR_StructInit->LPTMR_WorkMode = LPTMR_Time_Mode;
    LPTMR_StructInit->LPTMR_CountMode = LPTMR_Count_Wrap;
    LPTMR_StructInit->LPTMR_PinPolarity = LPTMR_Polarity_High;
    LPTMR_StructInit->LPTMR_InterruptEnable = LPTMR_Interrupt_Disable;
    LPTMR_StructInit->LPTMR_DmaRequest = LPTMR_DmaRequest_Disable;
    LPTMR_StructInit->LPTMR_BypassPrescaler = LPTMR_Prescaler_Disable;
    LPTMR_StructInit->LPTMR_Prescaler = LPTMR_PRESCALE_2;
    LPTMR_StructInit->LPTMR_CompareValue = 0xFFFF;
}

/**
 * @brief  Enable LPTMR.
 * @return None
 */
void LPTMR_Enable(void)
{
    LPTMR->CSR |= LPTMR_CSR_TEN_MASK;
}

/**
 * @brief  Disable LPTMR.
 * @return None
 */
void LPTMR_Disable(void)
{
    LPTMR->CSR &= ~(LPTMR_CSR_TEN_MASK);
}

/**
 * @brief  Get the DMA Request Enable Flag.
 * @return The new state of the LPTMR dma request (SET or RESET)
 */
FlagStatus_t LPTMR_GetDmaRequest(void)
{
    ITStatus_t bitstatus = RESET;
    
    if ((LPTMR->CSR & LPTMR_CSR_TDRE_MASK) == LPTMR_DmaRequest_Enable)
    {
        bitstatus = SET;
    }
    else
    {
        bitstatus = RESET;
    }
    return bitstatus;
}

/**
 * @brief  Get the Compare Flag state.
 * @return The new state of the LPTMR Compare Flag (SET or RESET)
 */
FlagStatus_t LPTMR_GetCompareFlag(void)
{
    ITStatus_t bitstatus = RESET;

    if ((LPTMR->CSR & LPTMR_CSR_TCF_MASK) == LPTMR_CSR_TCF_MASK)
    {
        bitstatus = SET;
    }
    else
    {
        bitstatus = RESET;
    }
    return bitstatus;
}

/**
 * @brief  Clear the Compare Flag.
 * @return None
 */
void LPTMR_ClearCompareFlag(void)
{
    LPTMR->CSR |= LPTMR_CSR_TCF_MASK;
}

/**
 * @brief  Get Prescaler/Glitch Filter divider value.
 * @return The Prescaler/Glitch filter value
 */
LPTMR_Prescaler_t LPTMR_GetPrescaler(void)
{
    uint32_t type = LPTMR->PSR & LPTMR_PSR_PRESCALE_MASK;

    return type;
}

/**
 * @brief  Get the Compare Value.
 * @return The Compare Value
 */
uint16_t LPTMR_GetCompareValue(void)
{
    return LPTMR->CMR;
}

/**
 * @brief  Configure the Compare Value.
 * @param  CompareValue: The new Prescaler value.
 * @return None
 */
void LPTMR_SetCompareValue(uint16_t CompareValue)
{
    LPTMR->CMR = CompareValue;
}

/**
 * @brief  Get the current Counter Value.
 * @return The Counter Value
 */
uint16_t LPTMR_GetCounterValue(void)
{
    return LPTMR->CNR;
}

/**
  * @}
  */

/**
 * @}
 */

/**
 * @}
 */
